﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊申请单服务
    /// </summary>
    public class OtpAppyFormService : OutpatientAppService, IOtpAppyFormService
    {
        private IRepository<OtpAppyForm, Guid> _otpAppyFormRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="OtpAppyFormRepository"></param>
        public OtpAppyFormService(IRepository<OtpAppyForm, Guid> otpAppyFormRepository)
        {
            _otpAppyFormRepository = otpAppyFormRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OtpAppyFormDto>> GetListAsync(OtpAppyFormInput input)
        {
            var iQuery = await _otpAppyFormRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpAppyForm>, List<OtpAppyFormDto>>(iQuery.ToList());

                return new PagedResultDto<OtpAppyFormDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpAppyForm>, List<OtpAppyFormDto>>(items.ToList());

                return new PagedResultDto<OtpAppyFormDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<OtpAppyFormDto> PostAsync(OtpAppyFormCreateOrUpdateDto input)
        {
            var otpAppyForm = ObjectMapper.Map<OtpAppyFormCreateOrUpdateDto, OtpAppyForm>(input);
            otpAppyForm.OtpRxRecId = Guid.NewGuid();
            otpAppyForm.AppyNo = "-";
            otpAppyForm = await _otpAppyFormRepository.InsertAsync(otpAppyForm);
            var itemDto = ObjectMapper.Map<OtpAppyForm, OtpAppyFormDto>(otpAppyForm);

            return itemDto;
        }
    }
}
